/*
 * Copyright (c) 2011 Nathan Byrd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.stackconfig.web.gwt.client.widgets;

import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Defines a component that can be added into stacks
 */
public class Component extends Composite {

    private static final int TAB_OFFSET_PIXELS = 7;
    private static final String INITIAL_NEXT_PANEL_SIZE = "35px";
    private PickupDragController dragController;

    SetWidgetDropController dropController;

    /**
     * Perform a clone of this widget
     *
     * @return Component clone
     */
    public Component cloneWidget() {
        return new Component(this.dragController);
    }

    interface ComponentUiBinder extends UiBinder<HTMLPanel, Component> {
    }

    private static ComponentUiBinder uiBinder = GWT.create(ComponentUiBinder.class);

    /**
     * Getter for the nextComponentPanel, where further components are stacked
     *
     * @return SimplePanel next component panel
     */
    public SimplePanel getNextComponentPanel() {
        return nextComponentPanel;
    }

    @UiField
    NextComponentPanel nextComponentPanel;

    @UiField
    HTMLPanel nextOuterWrapper;
    @UiField
    HTMLPanel topComponent;

    public Widget getTopComponent() {
        return topComponent;
    }

    /**
     * Constructor for a new Component.
     *
     * @param dragController PickupDragController main drag controller
     */
    public Component(PickupDragController dragController) {
        initWidget(uiBinder.createAndBindUi(this));
        nextComponentPanel.setParentComponent(this);
        nextOuterWrapper.setHeight(INITIAL_NEXT_PANEL_SIZE);
        this.dragController = dragController;
        setupDropController();

    }


    private void setupDropController() {
        this.dropController = new SetWidgetDropController(this);
        dragController.registerDropController(this.dropController);
    }

    private void removeDropController() {
        dragController.unregisterDropController(this.dropController);
        this.dropController = null;
    }

    /**
     * Called when a new component is added to this one
     */
    public void onComponentAdded() {
        removeDropController();
        onComponentResize();

    }

    public void onComponentResize() {
        // 7 offset is to account for negative offset due to "tabs"
        nextOuterWrapper.setHeight((nextComponentPanel.getWidget().getOffsetHeight() - TAB_OFFSET_PIXELS) + "px");

        // Look up the chain to find my parent Component
        Widget parent = this.getParent();
        while (parent != null && !(parent instanceof Component)) {
            parent = parent.getParent();
        }

        // If we found a parent
        if (parent != null) {
            ((Component) parent).onComponentResize();
        }

    }

    /**
     * Called when a component is removed from this one
     */
    public void onComponentRemoved() {
        nextOuterWrapper.setHeight(INITIAL_NEXT_PANEL_SIZE);
        setupDropController();
    }
}
